package com.shrct.businessprocess.purchase.impl;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.shrct.businessprocess.budget.RCBudget;
import com.shrct.businessprocess.product.RCProductEntry;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.purchase.RCBaseProductPurchaseBudget;
import com.shrct.businessprocess.purchase.RCBudgetContract;
import com.shrct.businessprocess.purchase.RCBudgetContractProductItem;
import com.shrct.businessprocess.purchase.RCIdentifiablePhysicalProductInStockEvent;
import com.shrct.businessprocess.purchase.RCProductAndQuantity;
import com.shrct.businessprocess.purchase.RCProductInStockEvent;
import com.shrct.businessprocess.purchase.RCdentifiableBudgetContractProductItem;
import com.shrct.businessprocess.purchase.impl.RCBudgetContractItemImpl;
import com.shrct.businessprocess.purchase.proxy.RCdentifiableBudgetContractProductItemProxy;
import java.util.Collection;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCDENTIFIABLEBUDGETCONTRACTPRODUCTITEM_TYPE")
public abstract class RCdentifiableBudgetContractProductItemImpl extends RCBudgetContractItemImpl implements RCdentifiableBudgetContractProductItem {
  public RCdentifiableBudgetContractProductItemImpl() {
    super();
  }
  
  public RCdentifiableBudgetContractProductItemImpl(final RCBudgetContract contract) {
    super( contract);
    
  }
  
  public Iterable<RCProductAndQuantity> getInStockItems() {
    Iterable<RCProductAndQuantity> _xblockexpression = null;
    {
      RCBudgetContract _contract = this.getContract();
      RCBudget _budget = _contract.getBudget();
      RCProductSpecification _productSpec = this.getProductSpec();
      Collection<? extends RCProductInStockEvent> _inStockEvents = ((RCBaseProductPurchaseBudget) _budget).getInStockEvents(_productSpec);
      Iterable<RCIdentifiablePhysicalProductInStockEvent> _filter = Iterables.<RCIdentifiablePhysicalProductInStockEvent>filter(_inStockEvents, RCIdentifiablePhysicalProductInStockEvent.class);
      final Function1<RCIdentifiablePhysicalProductInStockEvent,Boolean> _function = new Function1<RCIdentifiablePhysicalProductInStockEvent,Boolean>() {
          public Boolean apply(final RCIdentifiablePhysicalProductInStockEvent it) {
            RCBudgetContractProductItem _contractItem = it.getContractItem();
            boolean _equals = Objects.equal(_contractItem, RCdentifiableBudgetContractProductItemImpl.this);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<RCIdentifiablePhysicalProductInStockEvent> _filter_1 = IterableExtensions.<RCIdentifiablePhysicalProductInStockEvent>filter(_filter, _function);
      final Function1<RCIdentifiablePhysicalProductInStockEvent,RCProductEntry> _function_1 = new Function1<RCIdentifiablePhysicalProductInStockEvent,RCProductEntry>() {
          public RCProductEntry apply(final RCIdentifiablePhysicalProductInStockEvent it) {
            RCProductEntry _product = it.getProduct();
            return _product;
          }
        };
      final Iterable<RCProductEntry> inStockItems = IterableExtensions.<RCIdentifiablePhysicalProductInStockEvent, RCProductEntry>map(_filter_1, _function_1);
      final Function1<RCProductEntry,RCProductAndQuantity> _function_2 = new Function1<RCProductEntry,RCProductAndQuantity>() {
          public RCProductAndQuantity apply(final RCProductEntry product) {
            RCProductAndQuantity _rCProductAndQuantity = new RCProductAndQuantity();
            final Procedure1<RCProductAndQuantity> _function = new Procedure1<RCProductAndQuantity>() {
                public void apply(final RCProductAndQuantity it) {
                  it.setProduct(product);
                  Integer _count = product.getCount();
                  it.setQuantity(_count);
                }
              };
            RCProductAndQuantity _doubleArrow = ObjectExtensions.<RCProductAndQuantity>operator_doubleArrow(_rCProductAndQuantity, _function);
            return _doubleArrow;
          }
        };
      Iterable<RCProductAndQuantity> _map = IterableExtensions.<RCProductEntry, RCProductAndQuantity>map(inStockItems, _function_2);
      _xblockexpression = (_map);
    }
    return _xblockexpression;
  }
  
  public RCdentifiableBudgetContractProductItemProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCdentifiableBudgetContractProductItemProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCdentifiableBudgetContractProductItemProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
